const Config = require('../../config.js')
const AREAARR = require('../../utils/areaList.js')
Page({
  data: {
	formData: {
		userName: '',
		phonenumber : '',
		code: '',
		shareCode: '',
		password: '',
		provinceCode: '',
		cityCode: ''
	},
	rules: [
		{
            name: 'userName',
            rules: {required: true, message: '用户名必填'},
		},
		{
            name: 'phonenumber',
            rules: {required: true, message: '手机号必填'},
		},
		{
            name: 'code',
            rules: {required: true, message: '验证码必填'},
		},
		{
            name: 'password',
            rules: {required: true, message: '密码必填'},
		},
	],
	isAgree: true,
	provinceList:[],
	cityList:[],
	multiArray: [[], []],
	multiIndex: [0, 0],
	isDisabled: false,
	yzmText: '获取验证码',
	initArea: '',
	scene: ''
  },

	// 城市选择确定按钮 
	bindMultiPickerChange: function (e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		this.setData({
			multiIndex: e.detail.value,
			initArea: ''
		})
	},
	// 城市选择 滚动监听
	bindMultiPickerCitys: function (e) {
		var data = {
			multiArray: this.data.multiArray,
			multiIndex: this.data.multiIndex
		};
		data.multiIndex[e.detail.column] = e.detail.value;
		this.getCity(2,this.data.provinceList[1][data.multiIndex[0]])
		this.setData(data);
	},
	// 阅读并同意
	checkboxChange: function (e) {
		this.setData({
			isAgree: !!e.detail.value.length
		});
	},

	// 输入框数据变化监听
	formInputChange(e){
		const {field} = e.currentTarget.dataset;
		this.setData({
			[`formData.${field}`]: e.detail.value
		})
	},
	
	// 获取省市区
	getCity(leveType, parentId){
		wx.request({
			url: Config.host + "/system/sysCityInfoController/getCityWithOpenList",
			data: {
				leveType: leveType,
				parentId: parentId || ''
			},
			method: "GET",
			success: res => {
				if(res.data.code === "success"){
				let data = res.data.data;
				if(leveType ===  1){
					let citys = this.formatCity(data);
					this.setData({
						provinceList: citys
					})
					this.setData({
						multiArray: [citys[0],[]]
					})
					if(parentId === null){
						this.getCity(2, citys[1][0]);
					}
				}else {
					let citys = this.formatCity(data);
					this.setData({
						cityList: citys
					})
					this.setData({
						multiArray: [this.data.multiArray[0], citys[0]]
					})
				}
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	// 数组对象转数组
	formatCity(data){
		if(data.length === 0) return;
		let citys = [[],[]];
		for(let i=0; i<data.length;i++){
			citys[0].push(data[i].area_name);
			citys[1].push(data[i].id);
		}
		return citys
	},

	// 获取验证码
	// 18133600688 100537
	getYzm(){
		let phone = this.data.formData.phonenumber;
		var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/;
		if (!myreg.test(phone)) {
			wx.showToast({
				title: '手机号不正确！',
				icon: 'none'
			})
			return;
		} else if(!phone){
			wx.showToast({
				title: '请输入手机号！',
				icon: 'none'
			})
			return;
		}

		var maxtime = 1 * 60; // 1分钟
		this.setData({
			isDisabled: true,
			yzmText: maxtime + '秒'
		})
		let that = this;
		var timer = setInterval(function (){
			if (maxtime > 0) {
				--maxtime;
				let seconds = Math.floor(maxtime % 60);
				let msg = seconds + "秒";
				that.setData({
					yzmText: msg
				})
			} else{
			  clearInterval(timer);
			  that.setData({
				  isDisabled: false,
				  yzmText: '获取验证码'
			  })
			}
		  }, 1000);  
		wx.request({
			url: Config.host + "/system/userRegisterController/phoneRegister",
			data: {
				phone: phone
			},
			method: "GET",
			success: res => {
				if(res.data.code){
					wx.showToast({
						title: res.data.message,
						icon: 'none'
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	// 注册校验
	submitFn(){
		let errorText = '';
		if(this.data.formData.userName == ''){
			errorText = '请填写用户名';
		} else if(this.data.formData.phonenumber == ''){
			errorText = '请填写手机号';
		} else if(this.data.formData.code == ''){
			errorText = '请填写验证码';
		} else if(this.data.formData.password == ''){
			errorText = '请填写密码';
		}
		if(errorText){
			wx.showToast({
				title: errorText,
				icon: 'none'
			})
			return;
		}
		this.registerFn();
	},
	// 注册
	registerFn(){
		let cityIdx = this.data.multiIndex;
		if(!this.data.formData.cityCode){
			this.setData({
				[`formData.provinceCode`]: this.data.provinceList[1][cityIdx[0]],
				[`formData.cityCode`]: this.data.cityList[1][cityIdx[1]],
			})
		}
		wx.request({
			url: Config.host + "/system/userRegisterController/registerUserInfo",
			data: this.data.formData,
			method: "GET",
			success: res => {
				if(res.data.code === 'error'){
					wx.showToast({
						title: res.data.message,
						icon: 'none'
					})
					return;
				}
				wx.showToast({
					title: '注册成功！'
				})
				
				wx.redirectTo({
					url: '/pages/login/login'
				})
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	loginFn(){
		wx.redirectTo({
			url: '/pages/login/login'
		})
	},

  onLoad: function (options) {
	if(options.scene){
		let scene = decodeURIComponent(options.scene);
		// let scene = '1234&340000&340100';
		let paramsArr = scene.split('&');

		AREAARR.provinceList.forEach((item,index) => {
			if(item.code === paramsArr[1]){
				this.data.initArea = item.name;
			}
		})
		
		AREAARR.cityList[paramsArr[1]].forEach((item,index) => {
			if(item.code === paramsArr[2]){
				this.data.initArea += '-' + item.name;
			}
		})
		this.setData({
			'formData.shareCode' : paramsArr[0],
			'formData.provinceCode' : paramsArr[1],
			'formData.cityCode' : paramsArr[2],
			'initArea': this.data.initArea,
			'scene': scene
		})
	}else{
		this.getCity(1, null);
	}

	
  }
})
